package com.tesis_2.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


import com.example.tesis_2.R;
import com.tesis_2.BL.BLRestaurants;
import com.tesis_2.DA.DAOUtils;
import com.tesis_2.DA.ImageDownloader;
import com.tesis_2.MO.MORestaurant;
 
public class UTRestaurantsCategoryImageAdapter extends BaseAdapter {
	private Context context;
	
 
	public UTRestaurantsCategoryImageAdapter(Context context) {
		this.context = context;
		
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
		//LayoutInflater inflater = (LayoutInflater) context
		//	.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
 		View gridView;
 		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			gridView = new View(context);
			
		} else {
			//Toast.makeText(context, "null", 2).show();
			gridView = (View) convertView;
		}
		
		@SuppressWarnings("unused")
		MORestaurant rs = (MORestaurant) (getItem(position));
		// get layout from mobile.xml
		gridView = inflater.inflate(R.layout.activity_restaurants_category_item, null);
		
		//nombre del rest
		TextView textView = (TextView) gridView.findViewById(R.id.txvNombre_restaurant_category_item);
		textView.setText( ((MORestaurant) (getItem(position))).getName());
		
		//tipo de cocina
		textView = (TextView) gridView
				.findViewById(R.id.txvCuisine_restaurant_category_item);
		textView.setText( ((MORestaurant) (getItem(position))).getCuisine());
		
		//score
		RatingBar rb = (RatingBar) gridView.findViewById(R.id.rbCalificacion_restaurant_category_item);
		rb.setRating(((MORestaurant) (getItem(position))).getScore());
		
		// Aqui busco la imagen con el metodo asincrono, comentado para mejorar performance y pruebas
		ImageView imageView = (ImageView) gridView
			.findViewById(R.id.imgRest_restaurant_category_item);
		ImageDownloader mDownload = new ImageDownloader();
		
		mDownload.download(DAOUtils.getServer()+"/assets/restaurants/"+ ((MORestaurant) (getItem(position))).getId()  +"/small/" + ((MORestaurant) (getItem(position))).getPhoto(), imageView);
		imageView.setScaleType(ImageView.ScaleType.CENTER);
		return gridView;
	}
 
	@Override
	public int getCount() {
		return BLRestaurants.getLsRest().size();
	}
 
	@Override
	public Object getItem(int position) {
		return BLRestaurants.getLsRest().get(position);
	}
 
	@Override
	public long getItemId(int position) {
		return BLRestaurants.getLsRest().get(position).getId();
	}
 
}
